#include <iostream>
#include <thread>
#include <chrono>
#include "leomedlog/leomedlog.h"

void crash_function() {
    // 触发一个段错误来测试 Crashpad
    volatile int* p = nullptr;
    *p = 42;
}

int main(int argc, char* argv[]) {
    (void)argc;
    (void)argv;

    // 1. 配置
    leomedlog::LogConfig config;
    config.app_name = "helloworld";
    config.log_dir = "logs";
    config.crash_dir = "crashes";
    // config.sentry_dsn = ""; // 空 DSN 仅本地转储

    std::cout << "[Main] Initializing LeoMedLog..." << std::endl;

    // 2. 初始化
    leomedlog::Init(config);

    // 3. 业务日志测试
    LEO_LOG_INFO("Application started successfully");
    LEO_LOG_WARN("System load warning: {}%", 75);
    
    // 4. 结构化日志测试
    LEO_LOG_INFO_KV("User login action", 
        {"user_id", 10086}, 
        {"role", "admin"},
        {"ip", "192.168.1.100"}
    );

    LEO_LOG_WARN_KV("Disk space low", 
        {"path", "/var/log"}, 
        {"free_mb", 512}
    );

    // 5. 模拟运行
    std::this_thread::sleep_for(std::chrono::milliseconds(100));

    // 6. 崩溃测试 (可选，取消注释测试 Sentry/Crashpad)
    if (argc > 1 && std::string(argv[1]) == "--crash") {
        LEO_LOG_CRITICAL("Initiating self-destruct sequence...");
        // 确保日志刷盘
        leomedlog::GetLogger()->flush();
        crash_function();
    }

    std::cout << "[Main] Shutting down..." << std::endl;

    // 7. 退出
    leomedlog::Shutdown();
    return 0;
}
